package com.aw.baselib.widgets

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.view.View.OnClickListener
import android.widget.TextView
import androidx.appcompat.widget.Toolbar
import com.aw.baselib.R
import com.aw.baselib.util.AwDisplayUtil

/**
 * Created by hzw
 */
class AwViewCustomToolbar(private var mContext: Context, attrs: AttributeSet?, defStyleAttr: Int) :
    Toolbar(
        mContext, attrs, defStyleAttr
    ) {
    private var mTvLeftView: TextView? = null
    private var mTvMiddleView: TextView? = null
    private var mTvRightView: TextView? = null
    private var mToolbar: Toolbar? = null
    var mOnLeftClickListener: OnLeftClickListener? = null
    var mOnRightClickListener: OnRightClickListener? = null
    var mOnMiddleClickListener: OnMiddleClickListener? = null

    constructor(context: Context) : this(context, null) {
        mContext = context
    }

    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0) {
        mContext = context
    }

    init {
        inflate(mContext, R.layout.view_widgets_toolbar, this)
    }

    override fun onFinishInflate() {
        super.onFinishInflate()
        mTvLeftView = findViewById<View>(R.id.toolbar_left) as TextView
        mTvMiddleView = findViewById<View>(R.id.toolbar_title) as TextView
        mTvRightView = findViewById<View>(R.id.toolbar_right) as TextView
        mToolbar = findViewById<View>(R.id.toolbar) as Toolbar
        mTvLeftView!!.setOnClickListener(OnClickListener {
            if (mOnLeftClickListener == null) {
                return@OnClickListener
            }
            mOnLeftClickListener!!.onLeftTextClick()
        })
        mTvRightView!!.setOnClickListener(OnClickListener {
            if (mOnRightClickListener == null) {
                return@OnClickListener
            }
            mOnRightClickListener!!.onRightTextClick()
        })
        mTvMiddleView!!.setOnClickListener(OnClickListener {
            if (mOnMiddleClickListener == null) {
                return@OnClickListener
            }
            mOnMiddleClickListener!!.onMiddleClick()
        })
    }

    fun setToolbarBackgroundColor(color: Int) {
        mToolbar!!.setBackgroundResource(color)
    }

    /**
     * 设置只显示标题
     */
    fun setOnlyTitle() {
        mTvLeftView!!.visibility = INVISIBLE
        mTvRightView!!.visibility = INVISIBLE
    }

    /**
     * 设置标题不显示
     */
    fun hideTitleView() {
        mTvMiddleView!!.visibility = INVISIBLE
    }

    /**
     * 设置左侧不显示
     */
    fun hideLeftView() {
        mTvLeftView!!.visibility = INVISIBLE
    }

    /**
     * 设置右侧显示
     */
    fun showRightView() {
        mTvRightView!!.visibility = VISIBLE
    }

    /**
     * 设置右侧不显示
     */
    fun hideRightView() {
        mTvRightView!!.visibility = INVISIBLE
    }

    /**
     * 设置左侧文字颜色
     * @param colorRes
     */
    @SuppressLint("ResourceType")
    override fun setTitleTextColor(colorRes: Int) {
        mTvMiddleView!!.setTextColor(resources.getColor(colorRes))
    }

    /**
     * 设置左侧文字颜色
     * @param colorRes
     */
    fun setLeftTextColor(colorRes: Int) {
        mTvLeftView!!.setTextColor(resources.getColor(colorRes))
    }

    /**
     * 设置右侧文字颜色
     * @param colorRes
     */
    fun setRTextColor(colorRes: Int) {
        mTvRightView!!.setTextColor(resources.getColor(colorRes))
    }

    /**
     * 设置标题
     * @param text
     */
    fun setToolbarTitle(text: String?) {
        this.title = ""
        mTvMiddleView!!.visibility = VISIBLE
        mTvMiddleView!!.text = text
    }

    /**
     * 设置左侧文本
     * @param text
     */
    fun setLeftText(text: String?) {
        mTvLeftView!!.visibility = VISIBLE
        mTvLeftView!!.text = text

        //设置文本则不显示图片
        mTvLeftView!!.setCompoundDrawables(null, null, null, null)
    }

    /**
     * 设置右边文本
     * @param text
     */
    fun setRightText(text: String?) {
        mTvRightView!!.visibility = VISIBLE
        mTvRightView!!.text = text

        //设置文本则不显示图片
        mTvRightView!!.setCompoundDrawables(null, null, null, null)
    }

    /**
     * 设置右边文本
     * @param text
     */
    fun setRightTextWithColor(text: String?, colorRes: Int) {
        mTvRightView!!.visibility = VISIBLE
        mTvRightView!!.text = text
        mTvRightView!!.setTextColor(mContext.resources.getColor(colorRes))

        //设置文本则不显示图片
        mTvRightView!!.setCompoundDrawables(null, null, null, null)
    }

    /**
     * 设置左侧图片
     * @param id
     */
    fun setLeftImg(id: Int) {
        val drawable = resources.getDrawable(id)
        drawable.setBounds(0, 0, drawable.minimumWidth, drawable.minimumHeight)
        //        drawable.setBounds(0, 0, AwDisplayUtil.dip2px(mContext, 18), AwDisplayUtil.dip2px(mContext, 18));


        //设置图片则不显示文字
        mTvLeftView!!.text = "返回"
        //设置透明文字, 增大点击区域
        mTvLeftView!!.setTextColor(resources.getColor(R.color.transparent))
        mTvLeftView!!.setCompoundDrawables(drawable, null, null, null)
    }

    /**
     * 设置左侧图片(固定大小)
     * @param id
     */
    fun setLeftImgWithSizeValue(id: Int) {
        mTvLeftView!!.visibility = VISIBLE
        val drawable = resources.getDrawable(id)

        //设置图片则不显示文字
        mTvLeftView!!.text = ""
        mTvLeftView!!.setCompoundDrawables(drawable, null, null, null)
    }

    /**
     * 设置左侧图片含文字
     * @param id
     */
    fun setLeftImgAndText(id: Int, str: String?) {
        val drawable = resources.getDrawable(id)
        drawable.setBounds(0, 0, drawable.minimumWidth, drawable.minimumHeight)

        //设置图片则不显示文字
        mTvLeftView!!.text = str
        mTvLeftView!!.setCompoundDrawables(drawable, null, null, null)
    }

    /**
     * 设置右侧图片
     * @param id
     */
    fun setRightImg(id: Int) {
        val drawable = resources.getDrawable(id)
        drawable.setBounds(
            0,
            0,
            AwDisplayUtil.dip2px(mContext, 16f),
            AwDisplayUtil.dip2px(mContext, 16f)
        )

        //设置图片则不显示文字
        mTvRightView!!.text = ""
        mTvRightView!!.setCompoundDrawables(null, null, drawable, null)
    }

    /**
     * 设置右侧图片含文字
     * @param id
     */
    fun setRightImgAndText(id: Int, str: String?) {
        val drawable = resources.getDrawable(id)
        drawable.setBounds(0, 0, drawable.minimumWidth, drawable.minimumHeight)

        //设置图片则不显示文字
        mTvRightView!!.text = str
        mTvRightView!!.setCompoundDrawables(null, null, drawable, null)
    }

    //左侧文本回调接口
    interface OnLeftClickListener {
        fun onLeftTextClick()
    }

    /**
     * 设置左侧文本回调
     * @param listener
     */
    fun setOnLeftClickListener(listener: OnLeftClickListener?) {
        mOnLeftClickListener = listener
    }

    fun setOnMiddleClickListener(listener: OnMiddleClickListener?) {
        mOnMiddleClickListener = listener
    }

    //右侧文本回调接口
    interface OnRightClickListener {
        fun onRightTextClick()
    }

    interface OnMiddleClickListener {
        fun onMiddleClick()
    }

    /**
     * 设置右侧文本回调
     * @param litener
     */
    fun setOnRightClickListener(litener: OnRightClickListener?) {
        mOnRightClickListener = litener
    }

    /**
     * 设置返回图片
     * @param id 图片的id
     */
    fun setbackIcon(id: Int) {
        this.setNavigationIcon(id)
        mTvLeftView!!.visibility = GONE
        //左侧文本不设置draw
        mTvLeftView!!.setCompoundDrawables(null, null, null, null)
    }
}